<?php
namespace App\Http\Controllers\Client;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

class IndexController extends Controller
{
  //首页
  public function index()
  {
    // $ident = session('ident');
    if(Auth::check())
    $ident = Auth::user()->ident;
    else
    $ident = null;

    $nickname = DB::table('users')
                              ->select('nickname')
                              ->where('ident','=',$ident)
                              ->get();
        $avatar = DB::table('users')->select('avatar')->where('ident','=',$ident)->get();
    //展示帖子
    $lst = DB::table('article')
                  ->join('users','article.user_id','=','users.id')
                  ->select('users.*','article.*')
                  ->where('is_check','=','1')
                  ->orderBy('article.id', 'desc')
                  ->paginate(11);
    foreach($lst as $k=>$v){
      $temp = explode('#',$v->content_pic);
      $v->content_pic=$temp;
    }

    return view('index',compact('ident','nickname','lst','avatar'));
  }

  //详情页
  public function cardArticle1($id)
  {
    $ident = session('ident');
    $nickname = DB::table('users')
                              ->select('nickname')
                              ->where('ident','=',$ident)
                              ->get();
        $avatar = DB::table('users')->select('avatar')->where('ident','=',$ident)->get();
    $detail = DB::table('article')
                        ->join('users','article.user_id','=','users.id')
                        ->select('users.*','article.*')
                        ->where('article.id','=',$id)
                        ->get();
          foreach($detail as $k=>$v){
            $temp = explode('#',$v->content_pic);
            $v->content_pic=$temp;
          }

    $replylst = DB::table('reply')
                          ->join('users','reply.user_id','=','users.id')
                          ->select('users.nickname','users.sex','users.avatar','reply.*')
                          ->where('reply.article_id','=',$id)
                          ->orderBy('id', 'desc')
                          ->paginate(11);

    return view('cardArticle1',compact('ident','nickname','detail','replylst','avatar'));
  }

  //投稿
  public function fatie()
  {
    $ident = session('ident');
    $nickname = DB::table('users')
                              ->select('nickname')
                              ->where('ident','=',$ident)
                              ->get();
        $avatar = DB::table('users')->select('avatar')->where('ident','=',$ident)->get();
    return view('fatie',compact('ident','nickname','avatar'));
  }

  //ajax点赞
  public function ajaxdianzan(){
    $nowTime = Carbon::now('Asia/Shanghai');
    $ident = session('ident');
    $ids = DB::table('users')->select('id')->where('ident','=',$ident)->get();
    if(!isset($ids[0])){
      $arr = ['msg'=>'2'];
      echo json_encode($arr);
      die;
    }
    $user_id = $ids[0]->id;
    $arid = $_GET['arid'];
    $had = DB::table('iszan')->select('id')->where('user_id','=',$user_id)->where('article_id','=',$arid)->get();
    if(!isset($had[0])){
      $res = DB::table('iszan')->insert([
        'article_id'=>$arid,
        'user_id'=>$user_id,
        'create_time'=>$nowTime
      ]);
    }else{
      $res = 0;
    }
    if($res){
      $arr = ['msg'=>'1'];
      DB::table('article')->where('id','=',$arid)->increment('zannum');
    }else{
      $arr =['msg'=>'0'];
    }
    echo json_encode($arr);
  }

//ajax踩踩
public function ajaxchai(){
  $nowTime = Carbon::now('Asia/Shanghai');
  $ident = session('ident');
  $ids = DB::table('users')->select('id')->where('ident','=',$ident)->get();
      if(!isset($ids[0])){
        $arr = ['msg'=>'2'];
        echo json_encode($arr);
        die;
      }
  $user_id = $ids[0]->id;
  $arid = $_GET['arid'];
  $had = DB::table('ischai')->select('id')->where('user_id','=',$user_id)->where('article_id','=',$arid)->get();
  if(!isset($had[0])){
    $res = DB::table('ischai')->insert([
      'article_id'=>$arid,
      'user_id'=>$user_id,
      'create_time'=>$nowTime
    ]);
  }else{
    $res = 0;
  }
  if($res){
    $arr = ['msg'=>'1'];
    DB::table('article')->where('id','=',$arid)->decrement('zannum');
  }else{
    $arr = ['msg'=>'0'];
  }
  echo json_encode($arr);
}

  //ajax轮询进行消息推送
  public function msgsPush(){
    if(Auth::check()){
    $ident = session('ident');
    $user_id = DB::table('users')->where('ident','=',$ident)->first();

    $zans = DB::table('iszan')
                ->join('article','article.id','=','iszan.article_id')
                ->where('article.user_id','=',$user_id->id)
                ->where('is_read','=','0')
                ->get();

    $replys = DB::table('reply')
                ->join('article','article.id','=','reply.article_id')
                ->where('article.user_id',$user_id->id)
                ->where('is_read','=','0')
                ->get();

    $pushNum = $zans->count()+$replys->count();
    if($pushNum!=0){
      $arr = ['msg'=>'success','pushNum'=>$pushNum,'status'=>'1'];
      echo json_encode($arr);
    }else{
      $arr = ['msg'=>'empty','pushNum'=>0,'status'=>'0'];
      echo json_encode($arr);
    }
  }
}

}
